package com.c.ctgproject.adapter.file

import android.content.Context
import android.opengl.Visibility
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.Space
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseRecyclerViewAdapter
import com.c.ctgproject.entity.file.FileFormalitiesManagerEntity
import com.c.ctgproject.utils.handlerNull

class FileManagerAdapter(private val context: Context, private val mList: List<FileFormalitiesManagerEntity.DataBean.ListBean>, private val listener: FileManagerAdapterCallback) : BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_file_manager_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            "车辆编号: ${data.carCode.handlerNull()}".also { holder.mCarNumberTV.text = it }
            "车牌号: ${data.ownerPlate.handlerNull()}".also { holder.mCarPlateTV.text = it }
            "车主姓名: ${data.carOwner.handlerNull()}".also { holder.mOwnerNameTV.text = it }
            "车主电话: ${data.ownerPhone.handlerNull()}".also { holder.mOwnerPhoneTV.text = it }
            "入场时间: ${data.enterTime.handlerNull()}".also { holder.mInTimeTV.text = it }
            "车架号: ${data.carVin.handlerNull()}".also { holder.mCarVinTV.text = it }
            var showCheckBoxFlag=false
            //单独判断
            //回收证明是否已出（1 是 0 否）
            when (data.isRecycleProofProvided) {
                "1" -> {
                    holder.mReturnProveTV.visibility = View.VISIBLE
                    "回收证明: ${data.recycleProofTime.handlerNull()}".also { holder.mReturnProveTV.text = it }
                    holder.huishouluru.visibility = View.GONE
                }
                "0" -> {
                    showCheckBoxFlag=true
                    holder.mReturnProveTV.visibility = View.GONE
                    holder.huishouluru.visibility = View.VISIBLE
                }
                else -> {
                    holder.mReturnProveTV.visibility = View.GONE
                    holder.huishouluru.visibility = View.GONE
                }
            }
            //注销证明是否已出（1 是 0 否）
            when (data.isCancellationProofProvided) {
                "1" -> {
                    holder.mCertificationTV.visibility = View.VISIBLE
                    "注销证明: ${data.cancellationProofTime.handlerNull()}".also { holder.mCertificationTV.text = it }
                    holder.zhuxiaobanli.visibility = View.GONE
                }
                "0" -> {
                    showCheckBoxFlag=true
                    holder.mCertificationTV.visibility = View.GONE
                    holder.zhuxiaobanli.visibility = View.VISIBLE
                }
                else -> {
                    holder.mCertificationTV.visibility = View.GONE
                    holder.zhuxiaobanli.visibility = View.GONE
                }
            }
            //手续是否发放 1是 null否
            when (data.giveStatus) {
                "1" -> {
                    holder.shouxufafang.visibility = View.GONE
                }
                else -> {
                    showCheckBoxFlag=true
                    holder.shouxufafang.visibility = View.VISIBLE
                }
            }
            holder.mCheckBox.visibility=if (showCheckBoxFlag) View.VISIBLE else View.INVISIBLE
            holder.mCheckBox.isChecked = data.select
            holder.mCheckBox.setOnClickListener {
                listener.onCheckChange(position, holder.mCheckBox.isChecked)
            }
            holder.huishouluru.setOnClickListener {
                listener.onItemReceive(position)
            }

            holder.zhuxiaobanli.setOnClickListener {
                listener.onItemCancel(position)
            }
            holder.shouxufafang.setOnClickListener {
                listener.sendFormalities(position)
            }
            holder.mParentLL.setOnClickListener {
                listener.onManager(position)
            }
        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mCarNumberTV: TextView = view.findViewById(R.id.mCarNumberTV)
        val mCarPlateTV: TextView = view.findViewById(R.id.mCarPlateTV)
        val mOwnerNameTV: TextView = view.findViewById(R.id.mOwnerNameTV)
        val mOwnerPhoneTV: TextView = view.findViewById(R.id.mOwnerPhoneTV)
        val mCarVinTV: TextView = view.findViewById(R.id.mCarVinTV)
        val mInTimeTV: TextView = view.findViewById(R.id.mInTimeTV)
        val mReturnProveTV: TextView = view.findViewById(R.id.mReturnProveTV)
        val mCertificationTV: TextView = view.findViewById(R.id.mCertificationTV)
        val huishouluru: Button = view.findViewById(R.id.huishouluru)
        val zhuxiaobanli: Button = view.findViewById(R.id.zhuxiaobanli)
        val shouxufafang: Button = view.findViewById(R.id.shouxufafang)
        val mCheckBox: CheckBox = view.findViewById(R.id.mCheckBox)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
    }

    interface FileManagerAdapterCallback {
        fun onCheckChange(position: Int, isCheck: Boolean)

        //回收录入
        fun onItemReceive(position: Int)

        //注销
        fun onItemCancel(position: Int)

        //档案管理
        fun onManager(position: Int)

        //手续发放
        fun sendFormalities(position: Int)
    }
}